<?php
class CashbackAction extends CommonAction
{
	public function daily(){
		set_time_limit(0);
		$var=$this->_get('var');
		if($var == "sdgsadgeosgnedognospdg574g8sd4g2rfg4d8g7d8zghspgeorfr4657*w9+w"){
		
			$Count = D("CashbackCount");
			$Count->cashbackByDay();
			$Count->adjustByDay();
		
			$afflist = M('Affiliatesetting')->where(array('site_id' => __SITEID__, 'status' => 1))->find();
			 
			$afflist = unserialize($afflist['value']);
            $ponints_db = M('Points');
            $member_db = M('Member');
            $list = $ponints_db->where('total > '.$afflist['inte_rule'])->select();
            $inte_db=D('Integral_log');
			
			
			foreach ($list as $k => $v) {
				
				
				//exit(dump($v));
				//echo 1;
				$member=$member_db->where('id = '.$v['member_id'])->find();
			//	dump($member);
				$num=$v['total']/$afflist['inte_rule'];
				$inte=$v['total'];
				$v['total']=$afflist['inte_rule']*(int)$num;
                $total['total'] = ($v['total'] * ($afflist['inte_num']/100));
                //$data['online'] = 0;
                $data['offline'] = ($v['total'] * ($afflist['inte_num']/100));
				$total['total']=$inte-$total['total'];
				//$data['nowonline'] = $member['online']+$data['online'];
                // $data['nowoffline'] =$member['offline']+$data['offline'];
				$data['member_id']=$v['member_id'];
				$data['total']=$member['offline']+$data['offline'];
				$data['m_total']=$member['offline'];
		
				if($v['funds']>0){
				$f_data=$data;
				$f_data['offline'] =($v['funds']*($afflist['funds_pro']/100));
				$f_data['funds'] =$v['funds']-$f_data['offline'];
				
				$f_data['total']=$total['total']+$f_data['offline'];
				
				$f_data['m_total']+=$f_data['offline'];
				$data['m_total']+=$f_data['offline'];
				$inte_db->inset_log($f_data,2);
				M('Points')->where('member_id = '.$v['member_id'])->data($f_data)->save();	
				 }
			
				$data['m_total']+=$data['offline'];
				if($inte_db->inset_log($data)){
					
				
                if ($ponints_db->where(' member_id = ' . $v['member_id'])
                    ->data($total)
                    ->save()) {
					
                    $member_db->where('id = ' . $v['member_id'])
                        ->data(array('offline'=>$data['total']))
                        ->save();
                }
				}
			
            }
			
			//echo 1;
			
			import("@.ORG.Affiliate.Rangelevelbackmoney");
			$class = 'Rangelevelbackmoney';
			$Threelevel = new $class(0, 296);
			$Threelevel->moneyback();
			
			
		}
		
	}
	
	
	public function test(){
		
		exit();
		$afflist = M('Affiliatesetting')->where(array('site_id' => __SITEID__, 'status' => 1))->find();
			 
		$afflist = unserialize($afflist['value']);
		 $list = M('Points')->where('total > '.$afflist['inte_rule'])->select();
		 foreach($list as $k => $v){
			 if($v['funds']>0){
				 $f_data['back_integral'] =($v['funds']*($afflist['funds_pro']/100));
				$f_data['funds'] =$v['funds']-$f_data['back_integral'];
				
			M('Points')->where('member_id = '.$v['member_id'])->data($f_data)->save();	
				 }
			 
			 }
		 exit(dump($list));
	} 
	
	
	
	
	
	
	
	public function backmoney(){
		

		
		ini_set('memory_limit', '2048M');
		
		set_time_limit(0);
		
		
		$this->saledb = M('Sale');
		$product_sql = M('Cashback_config') -> where(array('site_id' => 156, 'total' => 0)) -> Field('product_id') -> buildSql();
		$list = $this->saledb -> where("product_id in(".$product_sql.") and pay_status = 1") -> order('id asc') -> group('member_id') -> Field('member_id, (sum(total)) as total,product_id') -> select();
		//dump($list);
		$db = M('Cashback_count');
		foreach($list as $k => $v){
			dump($v['total']);
			
			$data['已返'] = $db -> where(array('member_id' => $v['member_id'])) -> sum('total');
			//所有下级返现总额
			
			$data['需返'] = (float)$v['total'] * 40 * 0.021;
			
			
			
			//dump($data['需返']);
			
			
			
			$data['member_id'] = $v['member_id'];
			$sids = M('Member') -> where(array('pid' => $v['member_id'])) -> Field('id') -> select();
			
			
			
			foreach($sids as $sidsk => $sidsv){
				
				//他的所有下家返现消费
				$sale = $this->saledb -> where("product_id in(".$product_sql.") and pay_status = 1 and member_id in(".$sidsv['id'].")") -> Field('id,product_id,total,order_id') -> order('id asc') -> select();
				if($sale){ 
					//dump($sale);
				}
				$c = array();
				$c[] = $this->saledb -> where("product_id in(".$product_sql.") and pay_status = 1 and member_id in(".$sidsv['id'].")") -> order('id asc') -> sum('total');
				$c[] = $v['total'];
				if((float)min($c) <= 0){
					continue;
				}
				//dump(min($c) * 0.011 * 40);
				$data['需返'] += min($c) * 0.011 * 40;
				
			
}
			
			if(round((float)($data['需返'] - $data['已返']), 2) > 0){
					$insertdata['site_id'] = 156;
					$insertdata['member_id'] = $v['member_id'];
					$insertdata['total'] = $data['需返'] - $data['已返'];
					$insertdata['rest'] = $data['需返'] - $data['已返'];
					$insertdata['last_adjust'] = time();
					$insertdata['product_id'] = $v['product_id'];
					$insertdata['rate'] = 250;
					$insertdata['last_money'] = $data['需返'] - $data['已返'];
					M('Cashback_count') -> add($insertdata);
					dump($insertdata);
				}
			
			
			
			
		}

		
		
	
		
	}
	
	
	
	
	
	
	
	
	
	
} 
